layui.use(['form', 'table','layuimini','element', 'laydate'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoMarkDate = {
        id: 'BoMarkDateTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'title', title: '标题', align: 'center'},
            {field: 'markDateValue', title: '日期', width: 110, align: 'center'},
            {field: 'remarkType', title: '提醒类型', width: 100, align: 'center',
                templet: function (object) {
                    if(object.remarkType == 0){
                        return '<span class="layui-badge layui-bg-green">生日提醒</span>';
                    }else if(object.remarkType == 1){
                        return '<span class="layui-badge layui-bg-orange">纪念节日</span>';
                    }else if(object.remarkType == 2){
                        return '<span class="layui-badge layui-bg-blue">普通节日</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">备忘事项</span>';
                    }
                }
            },
            {field: 'dateType', title: '日期类型', width: 95, align: 'center',
                templet: function (object) {
                    if(object.dateType == 0){
                        return '<span class="layui-badge layui-bg-green">公历</span>';
                    }else{
                        return '<span class="layui-badge">农历</span>';
                    }
                }
            },
            {field: 'sendTime', title: '发送次数', width: 100, align: 'center',
                templet: function (object) {
                    if(object.sendTime == 0){
                        return '<span class="layui-badge layui-bg-green">五天三次</span>';
                    }else if(object.sendTime == 1){
                        return '<span class="layui-badge layui-bg-blue">五天两次</span>';
                    }else if(object.sendTime == 2){
                        return '<span class="layui-badge layui-bg-orange">当天发送</span>';
                    }
                }
            },
            {field: 'runTimeType', title: '提醒时间类型', width: 110, align: 'center',
                templet: function (object) {
                    if(object.runTimeType == 0){
                        return '<span class="layui-badge layui-bg-green">早上八点</span>';
                    }else if(object.runTimeType == 1){
                        return '<span class="layui-badge layui-bg-blue">早上九点</span>';
                    }else if(object.runTimeType == 2){
                        return '<span class="layui-badge layui-bg-orange">下午六点</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">晚上八点</span>';
                    }
                }
            },
            {field: 'personName', title: '本人昵称', width: 100, align: 'center'},
            {field: 'personPhone', title: '本人提醒手机号', width: 130, align: 'center'},
            {field: 'topic', title: '本人提醒主题(节日名称)', align: 'center'},
            {field: 'remindPhone', title: '额外提醒手机号', align: 'center'},
            {field: 'otherTopic', title: '额外提醒主题(节日名称)', align: 'center'},
            {field: 'isOpen', title: '开启提醒', width: 95, align: 'center',
                templet: function (object) {
                    if(object.isOpen == 0){
                        return '<input type="checkbox" lay-filter="l_isOpenSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '">';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isOpenSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" checked>';
                    }
                }
            },
            {field: 'remark', title: '备注', align: 'center',
                templet: function (object) {
                    if(object.remark == '' || object.remark == undefined || object.remark == null){
                        return '-';
                    }else{
                        return object.remark;
                    }
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 120, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoMarkDate.id,
        url: '/web/boMarkDate/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var title = $("#search_title").val();
        var remarkType = $("#search_remarkType").val();
        var sendTime = $("#search_sendTime").val();
        var personName = $("#search_personName").val();
        var personPhone = $("#search_personPhone").val();
        var markDate = $("#search_markDate").val();
        var isOpen = $("#search_isOpen").val();

        //封装
        var dataObject = {};
        dataObject['title'] = title;
        dataObject['remarkType'] = remarkType;
        dataObject['sendTime'] = sendTime;
        dataObject['personName'] = personName;
        dataObject['personPhone'] = personPhone;
        dataObject['markDate'] = markDate;
        dataObject['isOpen'] = isOpen;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoMarkDate.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_title").val("");
        $("#search_remarkType").val("-1");
        $("#search_sendTime").val("-1");
        $("#search_personName").val("");
        $("#search_personPhone").val("");
        $("#search_markDate").val("");
        $("#search_isOpen").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['title'] = "";
        dataObject['remarkType'] = "-1";
        dataObject['sendTime'] = "-1";
        dataObject['personName'] = "";
        dataObject['personPhone'] = "";
        dataObject['markDate'] = "";
        dataObject['isOpen'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoMarkDate.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    //时间
    laydate.render({
        elem: '#search_markDate'
    });

    // 监听添加操作
    $(".layuimini-container").on("click", "#add-btn", function () {
        var content = layuimini.getHrefContent('/web/boMarkDate/addOrEdit');
        var index = layer.open({
            title: '添加纪念日',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['600px','530px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoMarkDate.id);
            }
        });

        return true;
    });

    // 监听上一年操作
    $(".layuimini-container").on("click", "#last-year-btn", function () {
        layer.open({
            title: '刷新日期为往上一年'
            ,content: '全部日期年份将刷新为往上一年，是否确定操作？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMarkDate/refreshLastYear';
                $.post(url, {}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMarkDate.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return true;
    });

    // 监听下一年操作
    $(".layuimini-container").on("click", "#next-year-btn", function () {
        layer.open({
            title: '刷新日期为往下一年'
            ,content: '全部日期年份将刷新为往下一年，是否确定操作？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMarkDate/refreshNextYear';
                $.post(url, {}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMarkDate.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return true;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoMarkDate.edit = function(id){
        var content = layuimini.getHrefContent('/web/boMarkDate/addOrEdit?id=' + id);
        var index = layer.open({
            title: '编辑纪念日',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['600px','530px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoMarkDate.id);
            }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    BoMarkDate.delete = function(id){
        layer.open({
            title: '删除纪念日'
            ,content: '是否确定删除该纪念日？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boMarkDate/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoMarkDate.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //监听是否开放开关
    form.on('switch(l_isOpenSwitch)', function(data){
        var isOpen = 0;
        if(data.elem.checked){
            isOpen = 1;
        }

        var markDateId = data.value; //编号

        //更新状态
        var indexLoad = layer.load(2);
        var url = '/web/boMarkDate/updateIsOpen';
        $.post(url, {isOpen: isOpen, markDateId: markDateId}, function(data){
            layer.close(indexLoad);
            if(data.state == "200"){
                layer.msg(data.message, {icon: 1});
            }else{
                layer.msg(data.message, {icon: 2});
                //更新失败则改状态
                if(data.elem.checked){
                    data.elem.checked = false;
                }else{
                    data.elem.checked = true;
                }
                form.render();
            }
        })
    });



    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoMarkDate.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            BoMarkDate.delete(data.id);
        } else if (layEvent === 'edit') {
            BoMarkDate.edit(data.id)
        }
    });

});